// 时间戳格式化
export function date(timeStamp, format = 'yyyy-MM-dd HH:mm:ss') {
    var date = new Date(timeStamp * 1000)
    let year = date.getFullYear();
    let month = String(date.getMonth() + 1).padStart(2, '0');
    let day = String(date.getDate()).padStart(2, '0');
    let hour = String(date.getHours()).padStart(2, '0');
    let minute = String(date.getMinutes()).padStart(2, '0');
    let second = String(date.getSeconds()).padStart(2, '0');

    let obj = {
        'M+': month, // 月份  
        'd+': day, // 日  
        'h+': hour, // 小时  
        'm+': minute, // 分  
        's+': second, // 秒  
        'yyyy': year, // 年份  
        'MM': month,
        'dd': day,
        'HH': hour,
        'mm': minute,
        'ss': second
    };

    for (let k in obj) {
        if (new RegExp(`(${k})`).test(format)) {
            format = format.replace(RegExp.$1, (RegExp.$1.length === 1) ? (obj[k]) : (('00' + obj[k]).substr(('' + obj[k]).length)));
        }
    }
    return format;
}

// 把文件按二进制读取
export function readFile(file){
    return new Promise(resolve => {
        let reader = new FileReader();
        reader.readAsBinaryString(file);
        reader.onload = ev => {
            resolve(ev.target.result);
        };
    })
}
// 设置异步间隔延迟
export function delay(interval){
    return new Promise(resolve => {
        let timer = setTimeout(_ => {
            clearTimeout(timer);
            resolve();
        },interval)
    })
}
    

